import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    username: '',
    email: '',
    avatar: null,
    roles: [],
    isLogin: false,
  }),
  actions: {
    setUser({ username, email, avatar, roles }) {
      this.username = username
      this.email = email
      this.avatar = avatar
      this.roles = roles
      this.isLogin = true
    },
    getUserInfo() {
      return {
        username: this.username,
        email: this.email,
        avatar: this.avatar,
        roles: this.roles,
      }
    },
    updateUserInfo(data) {
      this.username = data.username
      this.email = data.email
      this.avatar = data.avatar
    },
    logout() {
      this.username = ''
      this.email = ''
      this.avatar = null
      ;(this.roles = []), (this.isLogin = false)
    },
  },
  persist: true,
})
